<style>
.layui-form-pane .layui-form-label{width:auto;}
.layui-form-item{margin-bottom: 0px;}
.order-time-bg{background-color:#1E9FFF;color:white;padding:0px 5px;}
.border-none{border: none}
.blue{ cursor: pointer}
.order-num span{display:inline-block;min-width:25px;margin:0px 2px;text-align:center;}
</style>
<div class="fixed-bar">
	<form class="layui-form layui-form-pane fixed-bar-form" action="{:url()}" method="get" id="hisi-table-search-optable">
	<div class="layui-form-item">	
		<div class="layui-inline">
			<label class="layui-form-label">订单号</label>
			<div class="layui-input-inline">
				<input type="text" name="client_ordernum" lay-verify="" placeholder="输入订单号" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">SKU</label>
			<div class="layui-input-inline">
				<input type="text" name="sku" lay-verify="" placeholder="输入SKU" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">所属工厂</label>
			<div class="layui-input-inline">
				{:Form::select('factory_id', '', $factoryKV, '', '')}
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">来源帐号</label>
			<div class="layui-input-inline">
				{:Form::select('is_auto_import', '', [''=> '请选择']+config('mimo.order_from'), '', '')}
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">订单状态</label>
			<div class="layui-input-inline">
				{:Form::select('order_status', '', ['' => '请选择', 1=>'未采购', 2=>'已采购', 3=>'已裁床', 4=>'已车缝', 5=>'已后整', 6=>'已出货', 7=>'已完成', 99=>'已废弃', 999=>'MES废除'], '', '')}
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">下单日期</label>
			<div class="layui-input-inline">
				<input type="text" name="create_date_range" readonly="" id="create_date_range" lay-verify="" placeholder="选择下单日期" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">完成日期</label>
			<div class="layui-input-inline">
				<input type="text" name="over_date_range" readonly="" id="over_date_range" lay-verify="" placeholder="选择完成日期" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">裁床日期</label>
			<div class="layui-input-inline">
				<input type="text" name="caichuang_over_date_range" readonly="" id="caichuang_over_date_range" lay-verify="" placeholder="选择裁床完成日期" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">订单排序</label>
			<div class="layui-input-inline">
				{:Form::select('orderby', '', ['' => '默认排序', '1' => '下单时间倒序', '2' => '下单时间升序', '3' => '货期时间倒序', '4' => '货期时间升序', '5' => '出货时间倒序', '6' => '出货时间升序', '7' => '完成时间倒序', '8' => '完成时间升序' ], '', '')}
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">单类</label>
			<div class="layui-input-inline">
				{:Form::select('type', '', [''=>'全部','normal'=>'返单','new'=>'首单','emergent'=>'紧急单','my_first'=>'奖励(第一次做不含尾货)','my_return_first'=>'初返(不含尾货)'], '', '')}
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">断码筛选</label>
			<div class="layui-input-inline">
				{:Form::select('break_type', '', [''=>'全部','1'=>'正常','2'=>'将断码','3'=>'已断码'],'','')}
			</div>
		</div>
		<div class="layui-inline layui-upload">
			<input type="hidden" name="stat_type">
			<input type="hidden" name="pwd" value="{$pwd}">
			<button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">搜索</button>
			<button type="button" class="layui-btn layui-btn-primary hisi-export">导出</button>
			<button type="button" class="layui-btn layui-btn-primary hisi-export" data-export="2">大货BOM导出</button>
            {if checkAuthByURI('mp/order/import') == true}
            <button type="button" class="layui-btn layui-btn-normal" id="test8">选择文件</button>
            <button type="button" class="layui-btn layui-btn-primary" id="test9">导入</button>
            <a href="__STATIC__/demo/order-import-demo.xls" class="layui-btn layui-btn-primary layui-btn-xs" target="_blank" title="点击下载导入的xls模板"><i class="layui-icon">&#xe702;</i></a>
            {/if}
			<span class="clear-search"></span>
            
		</div>
	</div>
	</form>
</div>

<!-- 进度统计 -->
<!--<div class="layui-collapse" lay-accordion>
	<div class="layui-colla-item">
		<h2 class="layui-colla-title">进度统计</h2>
		<div class="layui-colla-content layui-show">
			<table class="layui-table">
				<thead>
					<tr>
						<th></th>
						<th>采购</th>
						<th>裁剪</th>
						<th>车间</th>
						<th>尾部</th>
						<th>已打包</th>
						<th>合计</th>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td>件数/单数</td>
						<td><span class="red stat-cg-item">0</span> 件 / <span class="red stat-cg-order stat-type pointer" data-type="cg">0</span> 单</td>
						<td><span class="red stat-cj-item">0</span> 件 / <span class="red stat-cj-order stat-type pointer" data-type="cj">0</span> 单</td>
						<td><span class="red stat-chej-item">0</span> 件 / <span class="red stat-chej-order stat-type pointer" data-type="chej">0</span> 单</td>
						<td><span class="red stat-wb-item">0</span> 件 / <span class="red stat-wb-order stat-type pointer" data-type="wb">0</span> 单</td>
						<td><span class="red stat-dch-item">0</span> 件 / <span class="red stat-dch-order stat-type pointer" data-type="dch">0</span> 单</td>
						<td><span class="red stat-hj-item">0</span> 件 / <span class="red stat-hj-order stat-type pointer" data-type="hj">0</span> 单</td>
					</tr>
					<tr>
						<td>裁片外发</td>
						<td><span class="red cpwf-cg-item">0</span> 件 / <span class="red cpwf-cg-order stat-type pointer" data-type="cpwf_cg">0</span> 单</td>
						<td><span class="red cpwf-cj-item">0</span> 件 / <span class="red cpwf-cj-order stat-type pointer" data-type="cpwf_cj">0</span> 单</td>
						<td></td>
						<td></td>
						<td></td>
						<td><span class="red cpwf-hj-item">0</span> 件 / <span class="red cpwf-hj-order stat-type pointer" data-type="cpwf_hj">0</span> 单</td>
					</tr>
				</tbody>
			</table>
		</div>
	</div>
</div>-->

<table id="dataTable" lay-filter='dataTable'></table>

{include file="system@block/layui" /}

<script type="text/html" id="toolbar">
	<a href="javascript:void(0)" data-url="{:url('sync')}" title="同步指定日期订单" class="layui-btn layui-btn-primary layui-btn-sm sync-date"><i class="layui-icon">&#xe669;</i>同步指定日期订单</a>
	<a href="javascript:void(0)" data-url="{:url('sync')}" title="同步更新指定订单" class="layui-btn layui-btn-primary layui-btn-sm sync-order"><i class="layui-icon">&#xe666;</i>同步更新指定订单</a>
	<a href="/admin.php/mp/Order/diffStatus" class="layui-btn hisi-iframe" hisi-data="{width: '1500px', height: '95%'}" style="margin-left:40px;margin-right:8px;padding:0px 15px;background-color:#009688" title="<b>不同状态【{$diffStatusCount}条】</b>">
		不同状态<span style="color:#FFCC00">【{$diffStatusCount}条】</span>
	</a>
</script>
<div id="syncDateTpl" class="hide">
	<form class="layui-form layui-form-pane ml10 mt10 sync-date-form" action="{:url()}" method="get">
		<div class="layui-inline">
			<label class="layui-form-label">同步时间</label>
			<div class="layui-input-inline">
				<input type="text" name="sync_date_range" id="sync_date_range"  placeholder="选择同步时间" class="layui-input" style="width:300px;">
			</div>
		</div>
		<div class="layui-inline mt10">
			<label class="layui-form-label">同步账号</label>
			<div class="layui-input-inline">
				{:Form::checkbox('order_from', '', config('mimo.order_from'))}
			</div>
		</div>
	</form>
</div>

<script type="text/html" id="picTpl">
    <span class="lay-pic-container" src="{{ d.sInfo.pic }}">
		<img src="{{ d.sInfo.ori_pic }}" class="lay-pic">
		{{# if(d.status==99){ }}
			<span class="sign"><img src="/static/system/image/sign_delete.png"></span>
		{{# }else if(d.status==2){ }}
			<span class="sign"><img src="/static/system/image/sign_over.png"></span>
		{{# }else if(d.tail_status==1){ }}
			<span class="sign"><img src="/static/system/image/sign_tail.png"></span>
		{{# }else if(d.chejian_status==1){ }}
			<span class="sign"><img src="/static/system/image/sign_workshop.png"></span>
		{{# }else if(d.caichuang_status==1){ }}
			<span class="sign"><img src="/static/system/image/sign_cutting.png"></span>
		{{# }else if(d.caigou_status==1){ }}
			<span class="sign"><img src="/static/system/image/sign_purchasing_done.png"></span>
		{{# }else { }}
			<span class="sign"><img src="/static/system/image/sign_purchasing_todo.png"></span>
		{{# } }}
	</span>
</script>
<script type="text/html" id="skuTpl">
	{{# if(d.break_code != ''){ }}
	<span class="red">{{ d.break_code }}</span>
	<br>
	{{# } }}
    <span class="copy-item">{{ d.client_ordernum }}</span> <span class="layui-badge layui-bg-blue">{{ layui.tool.formatOrderOType(d.o_type) }}</span><span class="layui-badge layui-bg-orange">{{ layui.tool.formatOrderType(d.type) }}</span><br>
    <span class="copy-item">{{ d.sku }}</span>
	{{# if(d.type_ext == 'my_return_first'){ }}
	<span class="layui-badge layui-bg-green">初返</span>
	{{# }else if(d.type_ext == 'my_first'){ }}
	<span class="layui-badge layui-bg-green">第一次</span>
	{{# } }}
	<br>
    {{ d.shipment_time_diff }} <br>
    {{ d.create_time_diff }} <br>
    {{# if(d.status == 99){ }}<span class="layui-badge">{{ d.delete_text }}</span><br> {{# } }}
	{{ orderFrom[d.is_auto_import] }}<br>
	{{# if(d.client_status == 99){ }}
		<span class="red">{{ clientStatus[d.client_status] }}</span>
	{{# }else{ }}
		{{ clientStatus[d.client_status] }}
	{{# } }}
	
</script>
<script type="text/html" id="factoryTpl">
	工厂：{{ layui.tool.showSelect('factory_id', d.factory_id, factoryKV, 'factory-id', d.id) }}<br>
	{{# if(d.factory_type==0){ }}
	分组：{{ layui.tool.showSelect('group_id', d.group_id, d.groupKV, 'group-id', d.id, 'style="width:130px;"') }}<br>
	{{# }else{ }}
	分组：{{ layui.tool.showSelect('group_id', 0, d.selectEmpty, 'group-id', d.id, 'style="width:130px;"') }}<br>
	{{# } }}
	裁剪类型：{{ layui.tool.showSelect('caichuang_type', d.caichuang_type, caichuangTypeKV, 'caichuang-type', d.id) }}<br>

	{{# if(d.outgo_status == 0){ }}

	{{# if(d.factory_type==0){ }}
	外发：{{ layui.tool.showSelect('factory_outgo_id', d.group_id, d.selectEmpty, 'factory-outgo-id', d.id, 'style="width:130px;" factory-id="'+d.factory_id+'"') }}
	{{# }else{ }}
	外发：{{ layui.tool.showSelect('factory_outgo_id', d.order_outgo_group_id,d.groupKV, 'factory-outgo-id', d.id, 'style="width:130px;" factory-id="'+d.factory_id+'"') }}
	{{# } }}

	{{# } }}

	{{# if(d.outgo_status > 0){ }}
	外发：{{ layui.tool.showSelect('factory_outgo_id', d.factory_outgo_id, factoryOutgoGbFidsKV[d.factory_id], 'factory-outgo-id', d.id, ' style="width:130px;" factory-id="'+d.factory_id+'"') }}
	<!--    <span class="sync-factory-outgo-id blue" data-key="{{d.id}}"  factory-id="{{d.factory_id}}">刷新</span>-->
	<br>
	外发价格：<input type="text" value="{{ d.outgo_price }}" readonly="true" placeholder="请输入外发价格" autocomplete="off" class="layui-input outgo-price border-none" data-key="{{ d.id }}" style="display:inline-block;width:100px;">
	{{# } }}

</script>
<script type="text/html" id="orderNumTpl">
	<div class="order-num">
	{{# for(var i in d.osList){ }}
    	{{ d.osList[i].size_name }}：<span title="下单数">{{ d.osList[i].order_count }}</span>/<span title="裁床数">{{ d.osList[i].shicai_count }}</span>/<span title="车间数">{{ d.osList[i].jiaohuo_count }}</span>/<span title="后整数">{{ d.osList[i].jiaohuo_tail_count }}</span>/<span title="打包数">{{ d.osList[i].delivery_count }}</span>/<span title="出货数">{{ d.osList[i].daohuo_count }}</span> <br>
	{{# } }}
	总数：<span title="总下单数">{{ d.osStat.order_count }}</span>/<span title="总裁床数">{{ d.osStat.shicai_count }}</span>/<span title="总车间数">{{ d.osStat.jiaohuo_count }}</span>/<span title="总后整数">{{ d.osStat.jiaohuo_tail_count }}</span>/<span title="总打包数">{{ d.osStat.delivery_count }}</span>/<span title="总出货数">{{ d.osStat.daohuo_count }}</span>
	</div>
</script>
<script type="text/html" id="orderTimeTpl">
    <div class="{{# if(d.create_time > 0){ }}order-time-bg{{# } }}">下单：{{# if(d.create_time > 0){ }} {{ layui.util.toDateString(d.create_time * 1000) }} {{# } }}</div>
	<div class="{{# if(d.shipment_time > 0){ }}order-time-bg{{# } }}">货期：{{# if(d.shipment_time > 0){ }} {{ layui.util.toDateString(d.shipment_time * 1000) }} {{# } }}</div>    
	<div class="{{# if(d.caigou_over_time > 0){ }}order-time-bg{{# } }}">采购：{{# if(d.caigou_over_time > 0){ }} {{ layui.util.toDateString(d.caigou_over_time * 1000) }} {{# } }}</div>
    <div class="{{# if(d.caichuang_over_time > 0){ }}order-time-bg{{# } }}">裁床：{{# if(d.caichuang_over_time > 0){ }} {{ layui.util.toDateString(d.caichuang_over_time * 1000) }} {{# } }}</div>
    <div class="{{# if(d.chejian_over_time > 0){ }}order-time-bg{{# } }}">车间：{{# if(d.chejian_over_time > 0){ }} {{ layui.util.toDateString(d.chejian_over_time * 1000) }} {{# } }}</div>
    <div class="{{# if(d.tail_over_time > 0){ }}order-time-bg{{# } }}">尾部：{{# if(d.tail_over_time > 0){ }} {{ layui.util.toDateString(d.tail_over_time * 1000) }} {{# } }}</div>
    <div class="{{# if(d.over_time > 0){ }}order-time-bg{{# } }}">总完：{{# if(d.over_time > 0){ }} {{ layui.util.toDateString(d.over_time * 1000) }} {{# } }}</div>
</script>
<script type="text/html" id="orderUserTpl">
	{{ d.caigou_user_name }} <br>
    {{ d.caichuang_user_name }}
</script>
<script type="text/html" id="buttonTpl">
	<div style="line-height:35px;">
	{{# if(d.status < 2){ }}
		<a href="{:url('status')}?order_id={{ d.id }}&status=2" title="订单完成" class="layui-btn layui-btn-sm hisi-tr-del" confirm="您确定要执行该操作吗？">订单完成</a><br>
		<a href="{:url('status')}?order_id={{ d.id }}&status=99" title="订单废弃" class="layui-btn layui-btn-sm hisi-tr-del layui-btn-danger" confirm="您确定要执行该操作吗？">订单废弃</a><br>
	{{# } }}
	{{# if(d.status < 2 && d.outgo_status == 0){ }}
		<a href="{:url('outgoStatus')}?order_id={{ d.id }}" title="裁片外发" class="layui-btn layui-btn-sm hisi-ajax layui-btn-warm" confirm="您确定要执行该操作吗？" refresh="#hisi-table-search">裁片外发</a><br>
	{{# } }}
	<a href="{:url('print')}?order_id={{ d.id }}" title="订单打印" class="layui-btn layui-btn-sm layui-btn-normal" target="_blank">订单打印</a><br>
	{if checkAuthByURI('mp/order/edit') == true}
	<a href="{:url('edit')}?order_id={{ d.id }}" class="layui-btn layui-btn-sm layui-btn-primary hisi-iframe" hisi-data="{width: '70%', height: '70%'}" title="订单编辑">订单编辑</a><br>
	{/if}
	<a href="{:url('materialLog')}?client_ordernum={{ d.client_ordernum }}&alert=1" hisi-data="{width: '70%', height: '50%'}" class="layui-btn layui-btn-sm layui-btn-primary hisi-iframe" title="材料操作日志">材料操作日志</a>
	</div>
</script>
<script type="text/javascript">
	var factoryKV = {:json_encode($factoryKV);};
	var groupKV = {:json_encode($groupKV);};
    var factoryOutgoGbFidsKV = {:json_encode($factoryOutgoGbFidsKV);};
	groupKV['0'] = '请选择';
	var factoryOutgoSelect = {:json_encode($factoryOutgoSelect);};
	var caichuangTypeKV = {:json_encode($caichuangTypeKV);};
	caichuangTypeKV['0'] = '请选择';
	var orderFrom = {:json_encode($orderFrom);};
	var clientStatus = {:json_encode($clientStatus);};
	layui.use([ 'opTable', 'jquery', 'tool', 'laydate', 'laytpl', 'form', 'layer','upload'], function() {
		var $ = layui.jquery;
		var laydate = layui.laydate;
		var laytpl = layui.laytpl;
		var form = layui.form;
		var layer = layui.layer;
        var upload = layui.upload;
		laydate.render({
		    elem: '#create_date_range'
		    ,range: '~'
		    ,theme: 'molv'
		    ,mark: {'{:$today}':''}
	    	,change: function(value, date, endDate){
		    	$('#create_date_range').val(value);
            	$('#layui-laydate1').remove();//关闭面板
            }
		});
		laydate.render({
		    elem: '#over_date_range'
		    ,range: '~'
		    ,theme: 'molv'
		    ,mark: {'{:$today}':''}
		    ,change: function(value, date, endDate){
		    	$('#over_date_range').val(value);
            	$('#layui-laydate2').remove();//关闭面板
            }
		});
		laydate.render({
		    elem: '#caichuang_over_date_range'
		    ,range: '~'
		    ,theme: 'molv'
		    ,mark: {'{:$today}':''}
		    ,change: function(value, date, endDate){
		    	$('#caichuang_over_date_range').val(value);
            	$('#layui-laydate3').remove();//关闭面板
            }
		});
		
		var opTable = layui.opTable.render({
			elem : '#dataTable'
			,url : '{:url()}'
			,page: true
			,limit: 20
            ,limits: [20,50,100,150,200]
            ,text: {
                none : '暂无相关数据'
            }
			,openVisible: false
			,toolbar: '#toolbar'
	        ,defaultToolbar: ['filter', 'exports', 'print']
			,cols : [[ 
				{field:'pic',title : '图片', templet:'#picTpl',width:110}
				,{field:'client_ordernum',title : '订单号/SKU/时效', templet:'#skuTpl',width:215}
				,{field:'factory_id',title : '工厂/分组', templet:'#factoryTpl',width:240}
				,{field:'order_num',title : '下单 / 裁床 / 车间 / 后整 / 打包 / 出货', templet:'#orderNumTpl',width:320}
				,{field:'order_time',title : '时间进度', templet:'#orderTimeTpl',width:220}
				,{field:'order_user',title : '采购执行人/裁床执行人', templet:'#orderUserTpl'}
				,{field:'remark',title : '备注',edit:'text',style:'border-bottom:2px dotted #5FB878;'}
				,{field:'',title : '操作',templet:'#buttonTpl'}
			]]

			,onEdit: function(obj) {
				var value = obj.value //得到修改后的值
			    ,data = obj.data //得到所在行所有键值
			    ,field = obj.field; //得到字段
			    
			    // 编辑备注
			    if (field == 'remark') {
			    	$.ajax({
			 		   type: "POST",
			 		   url: "{:url('remark')}",
			 		   data: "order_id="+data.id+"&remark="+value,
			 		   dataType:"json",
			 		   success: function(res){
			 			  if (res.code == 1) {
							 layer.msg(res.msg, {icon: 1});
						  } else {
							 layer.msg(res.msg, {icon: 2,anim: 6});
						  }
			 		   }
			 		});
			    }
			}
			
			,done:function(res) {
            	var html = '';
            	html += '<span>总款数：';
            	html += res.stat.skuCount;
            	html += '，</span>';
            	html += '<span>总下单:';
            	html += res.stat.itemsCount;
            	html += ' /裁床:'+res.ordertotal.shicai_count+' / 车间:'+res.ordertotal.jiaohuo_count+' / 后整:'+res.ordertotal.jiaohuo_tail_count+' / 打包:'+res.ordertotal.delivery_count+' / 出货:'+res.ordertotal.daohuo_count+' </span>';
            	$('.layui-laypage-default').append(html);
            	
            	// 订单进度统计
//            	if (res.stat.progress !== undefined) {
//            		var p = res.stat.progress;
//            		$('.stat-cg-item').html(p.cg[1]);
//            		$('.stat-cj-item').html(p.cj[1]);
//            		$('.stat-chej-item').html(p.chej[1]);
//            		$('.stat-wb-item').html(p.wb[1]);
//            		$('.stat-dch-item').html(p.dch[1]);
//            		$('.stat-hj-item').html(p.hj[1]);
//            		$('.cpwf-cg-item').html(p.cpwf[1]);
//            		$('.cpwf-cj-item').html(p.cpwf[5]);
//            		
//            		$('.stat-cg-order').html(p.cg[2]);
//            		$('.stat-cj-order').html(p.cj[2]);
//            		$('.stat-chej-order').html(p.chej[2]);
//            		$('.stat-wb-order').html(p.wb[2]);
//            		$('.stat-dch-order').html(p.dch[2]);
//            		$('.stat-hj-order').html(p.hj[2]);
//            		$('.cpwf-cg-order').html(p.cpwf[2]);
//            		$('.cpwf-cj-order').html(p.cpwf[6]);
//            	}
            }
		});
		
		// 设置工厂
		$(document).on('change','.factory-id',function(){
			var factoryId = $(this).val();
			var orderId = $(this).attr('data-key');
			if (factoryId) {
				$.ajax({
		 		   type: "POST",
		 		   url: "{:url('setFactoryId')}",
		 		   data: "factory_id="+factoryId+"&order_id="+orderId,
		 		   dataType:"json",
		 		   success: function(res){
		 			  if (res.code == 1) {
		 				 var groupKVNew = res.data.groupKVNew;
                         var factoryOutgoKV = res.data.factoryOutgoKV;
		 				 var option = '',option1 = '';
		 				 for (var x in groupKVNew) {
		 					 option = option + '<option value="'+x+'">'+groupKVNew[x]+'</option>';
		 				 }
						  if( res.data.type==0){
							  $('#group_id-'+orderId).html(option);
							  $('#factory_outgo_id-'+orderId).html('<option value="0">请选择</option>');
						  }else{
							  $('#group_id-'+orderId).html( '<option value="0">请选择</option>');
							  $('#factory_outgo_id-'+orderId).html(option);
						  }
						 layer.msg(res.msg, {icon: 1});
					  } else {
						 layer.msg(res.msg, {icon: 2,anim: 6});
					  }
		 		   }
		 		});
			}
		});
		
		// 设置分组
		$(document).on('change','.group-id',function(){
			var groupId = $(this).val();
			var orderId = $(this).attr('data-key');
			if (groupId > 0) {
				$.ajax({
		 		   type: "POST",
		 		   url: "{:url('setGroupId', ['_t' => 1])}",
		 		   data: "group_id="+groupId+"&order_id="+orderId,
		 		   dataType:"json",
		 		   success: function(res){
		 			  if (res.code == 1) {
						 layer.msg(res.msg, {icon: 1});
					  } else {
						 layer.msg(res.msg, {icon: 2,anim: 6});
					  }
		 		   }
		 		});
			}
		});
		
		// 设置裁剪类型
		$(document).on('change','.caichuang-type',function(){
			var caichuangType = $(this).val();
			var orderId = $(this).attr('data-key');
			if (caichuangType > 0) {
				$.ajax({
		 		   type: "POST",
		 		   url: "{:url('setCaichuangType')}",
		 		   data: "caichuang_type="+caichuangType+"&order_id="+orderId,
		 		   dataType:"json",
		 		   success: function(res){
		 			  if (res.code == 1) {
						 layer.msg(res.msg, {icon: 1});
					  } else {
						 layer.msg(res.msg, {icon: 2,anim: 6});
					  }
		 		   }
		 		});
			}
		});
		
		// 设置外发工厂
		$(document).on('change','.factory-outgo-id',function(){
			var factoryOutgoId = $(this).val();
			var orderId = $(this).attr('data-key');
			if (factoryOutgoId > 0) {
				$.ajax({
		 		   type: "POST",
		 		   url: "{:url('setFactoryOutgoId', ['_t' => 1])}",
		 		   data: "factory_outgo_id="+factoryOutgoId+"&order_id="+orderId,
		 		   dataType:"json",
		 		   success: function(res){
		 			  if (res.code == 1) {
						 layer.msg(res.msg, {icon: 1});
					  } else {
						 layer.msg(res.msg, {icon: 2,anim: 6});
					  }
		 		   }
		 		});
			}
		});
		// 刷新外发工厂选择框
		$(document).on('click','.sync-factory-outgo-id',function(){            
			var factoryId = $(this).attr('factory-id');
			var orderId = $(this).attr('data-key');console.log(factoryId);
            $('#factory_outgo_id-'+orderId).html("");
			if (factoryId > 0) {
				$.ajax({
		 		   type: "POST",
		 		   url: "{:url('getFactoryOutgoSelect')}",
		 		   data: "factory_id="+factoryId,
		 		   dataType:"json",
		 		   success: function(res){
		 			  if (res.code == 1) {
                         var factoryOutgoKV = res.data.factoryOutgoKV;
		 				 var option1 = '';
                         for (var i in factoryOutgoKV) {
		 					 option1 = option1 + '<option value="'+i+'">'+factoryOutgoKV[i]+'</option>';
		 				 }
		 				 $('#factory_outgo_id-'+orderId).html(option1);
						 layer.msg(res.msg, {icon: 1});
					  } else {
						 layer.msg(res.msg, {icon: 2,anim: 6});
					  }
		 		   }
		 		});
			}
		});
		// 设置外发价格
//		$(document).on('blur','.outgo-price',function(){
//			var reg = /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/;
//			var price = $(this).val();
//			var orderId = $(this).attr('data-key');
//			if (reg.test(price)) {
//				$.ajax({
//		 		   type: "POST",
//		 		   url: "{:url('setFactoryOutgoPrice')}",
//		 		   data: "price="+price+"&order_id="+orderId,
//		 		   dataType:"json",
//		 		   success: function(res){
//		 			  if (res.code == 1) {
//						 layer.msg(res.msg, {icon: 1});
//					  } else {
//						 layer.msg(res.msg, {icon: 2,anim: 6});
//					  }
//		 		   }
//		 		});
//			} else {
//				layer.msg('外发价格输入错误', {icon: 2,anim: 6});
//			}
//		});
		
		// 同步指定日期订单
		$(document).on('click','.sync-date',function(){
			var url = $(this).attr('data-url');
			var content = $('#syncDateTpl');
			$('.sync-date-form')[0].reset();
			
			var layerSyncDate = layer.open({
				type: 1
				,shade: 0
				,offset: '100px'
				,anim: parseInt(Math.random()*(6+1),10)
				,area: ['450px', '200px']
				,skin:'layui-layer-molv'
				,title:'同步指定时间段订单'
				,content: content
				,btn: ['确定']
			  	,yes: function(index, layero){
			  		var layerLoad = layer.load(0, {offset: '100px',shade: [0.5,'#F2F2F2']});
			  		//console.log(url);
		    		$.ajax({
		 	 		   type: "POST",
		 	 		   url: url,
		 	 		   data: $('.sync-date-form').serialize(),
		 	 		   dataType:"json",
		 	 		   success: function(res){
		 	 			  layer.close(layerLoad);
		 	 			  if (res.code == 1) {
		 	 				 layer.close(layerSyncDate);
		 					 layer.msg(res.msg, {icon: 1});
		 				  } else {
		 					 layer.msg(res.msg, {icon: 2,anim: 6});
		 				  }
		 	 		   }
		 	 		});
			  	}
				,success: function(){
					laydate.render({
					    elem: '#sync_date_range'
					    ,trigger: 'click'
					    ,range: '~'
						,theme: 'molv'
						,type: 'datetime'
						,mark: {'{:$today}':''}
						,btns: ['clear', 'confirm']
						,change: function(value, date, endDate){
							 $('#sync_date_range').val(value);
							 //$('#layui-laydate4').remove();//关闭面板
					    }
					});
					form.render();
				}
			});
		});
		
		// 同步指定订单
		$(document).on('click','.sync-order',function(){
			window.syncOrder = function(clientOrdernum) {
				var layerLoad = layer.load(0, {offset: '100px',shade: [0.5,'#F2F2F2']});
				$.ajax({
		 		   type: "POST",
		 		   url: "{:url('sync')}",
		 		   data: "client_ordernum="+clientOrdernum,
		 		   dataType:"json",
		 		   success: function(res){
		 			  layer.close(layerLoad);
		 			  if (res.code == 1) {
						 layer.msg(res.msg, {icon: 1});
						 layer.close(layerSyncOrder);
					  } else {
						 layer.msg(res.msg, {icon: 2,anim: 6});
					  }
		 		   }
		 		});
			}
			
			var layerSyncOrder = layer.prompt({title: '请输入订单号', formType: 0,success:function(){
	            // 键盘事件，判断回车
	            $("input.layui-layer-input").on('keydown',function(e){
	                if (e.which == 13) {
	                	syncOrder($(this).val());
	                }
	            });
			}}, 
			function(number){
				syncOrder(number);
			});
			
		});
		
		// 订单进度订单数点击
		$(document).on('click', '.stat-type', function() {
			var type = $(this).attr('data-type');
			var num = $(this).html();
			if (num > 0) {
				$('input[name=stat_type]').val(type);
				$("#hisi-table-search-optable").trigger("submit");
				$(".clear-search").html('<a href="javascript:void(0)" class="clear-search-btn red"><i class="layui-icon">&#xe640;</i>清除当前搜索条件</a>');
			}
		});
		// 清除搜索条件
		$(document).on('click', '.clear-search-btn', function() {
			$('input[name=stat_type]').val('');
			$(this).remove();
			$("#hisi-table-search-optable").trigger("submit");
		});
		
		/**
	     * layui非静态table搜索渲染
	     * @attr data-table table容器ID
	     * @attr hisi-data table基础参数
	     * @attr action 搜索请求地址
	     */
	   $(document).on('submit', '#hisiSearchOpTable,#hisi-table-search-optable', function() {
	        var that = $(this), 
	            arr = that.serializeArray(), 
	            where = new Array(),
	            dataTable = that.attr('data-table') ? that.attr('data-table') : 'dataTable',
	            options = new Function('return '+ that.attr('hisi-data'))() || {page: {curr:1}};
	        
	            for(var i in arr) {
	                where[arr[i].name] = arr[i].value;
	            }
	        
	        options.url = that.attr('action');
	        options.where = where;
	        
	        opTable.reload(options);
	        return false;
	    });
		/*
         * 上传导入Excel
         */
        upload.render({
            elem: '#test8'
            ,url: "{:url('import')}" //改成您自己的上传接口
            ,auto: false
            //,multiple: true
            ,bindAction: '#test9'
            , accept: 'file'
            ,done: function(res){
                if(res.code==2){
                    var order=res.data.order,sku=res.data.sku,size=res.data.size;
                    var content="";
                    var countOrder = order.length;
                    var countSku = sku.length;
                    var countSize = size.length;
                    content += '<tr>';
                    content += '<td>已存在的订单</td><td>';
					 for (var i in order){
						 content += '<span class="copy-item">' + order[i] + '<span></br>';
					 }
                    content +='</td></tr>';
                    content += '<tr>';
                    content += '<td>不存在的sku</td><td>';
					 for (var i in sku){
						 content += '<span class="copy-item">' + sku[i] + '<span></br>';
					 }
                    content +='</td></tr>';
                    content += '<tr>';
                    content += '<td>不存在的尺码</td><td>';
					 for (var i in size){
						 content += '<span class="copy-item">' + size[i] + '<span></br>';
					 }
                    content +='</td></tr>';
                    layer.open({
                        type: 1
                        ,title:'导入失败请完善'
                        ,offset: 'auto' //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
                        ,id: 'layerDemoauto' //防止重复弹出
                        ,content: '<table class="layui-table">'+content+'</table>'
                        ,btn: '关闭全部'
                        ,btnAlign: 'c' //按钮居中
                        ,shade: 0 //不显示遮罩
                        ,yes: function(){
                          layer.closeAll();
                        }
                    });
                }else{
                    layer.msg(res.msg);
                }
            }
        });
	});
</script>